function imgcmp(cmd, n)
%
% function to compare images across datac data files and tiff stack analyses.
% The idea is to plot the time course of calcium data against the time course
% of the cell voltage.
%
% This is a wrapper for image_compare.
% 0. Look for the physiology data file expt
% 1. find the corresponding file "filename.map".
% 2. read the map file - it contains the correspondence between records in datac
% 		and the files (.mat format) stored from Scott's aif analysis program (which does tiffs too).
% 3. build a drop-down list for the correspondence.
% 4. WHen the user selects an item from the list, and hits the 'display' button,
%		display the data using image_compare.
% That's all.

persistent X

basepath = '/users/pmanis/Documents/CurrentProjects';

h = findobj('Tag', 'image_compare'); % look for our window
if(isempty(h))
   h = figure('name', 'Compare Imaging and Physiology', ...
      'BackingStore', 'on', ...
      'NumberTitle', 'off', ...
      'Units', 'characters', ...
      'MenuBar', 'none', ...
      'WindowButtonMotionFcn', 'datac(''mouse_motion'', gcbf);', ...
      'WindowButtonDownFcn', 'datac(''mouse_down'', gcbf);', ...
      'WindowButtonUpFcn', 'datac(''mouse_up'', gcbf);', ...
      'PaperOrientation', 'Landscape', ...
      'Tag','image_compare');   % build the display information.
   f1 = uimenu('Label', '&File', 'Position', 1);
   uimenu(f1, 'Label', 'Phys&Open', 'Callback', 'imgcmp open;');
   uimenu(f1, 'Label', '&Accumulate', 'Callback', 'imgcmp accumulate;', ...
      'Separator', 'on', ...
      'Tag', 'accumulate', ...
      'Check', 'off');
   uimenu(f1, 'Label', '&Clear Display', 'Callback', 'imgcmp clear;');
   uimenu(f1, 'Label', 'Pge &Setup', 'Callback', 'imgcmp pagesetup;', 'Separator', 'on');
   uimenu(f1, 'Label', 'P&rinter Setup', 'Callback', 'imgcmp printsetup;', 'Separator', 'off');
   uimenu(f1, 'Label', '&Print', 'Callback', 'imgcmp print;', 'Separator', 'off');
   uimenu(f1, 'Label', 'Export .fig', 'Callback', 'imgcmp export-fig;', 'Separator', 'on');
   uimenu(f1, 'Label', 'Export .ill', 'Callback', 'imgcmp export-ill;', 'Separator', 'off');
   uimenu(f1, 'Label', 'Export .m', 'Callback', 'imgcmp export-m;', 'Separator', 'off');
   uimenu(f1, 'Label', 'Export .wmf', 'Callback', 'imgcmp export-wmf;', 'Separator', 'off');
   uimenu(f1, 'Label', 'Export .epsc2', 'Callback', 'imgcmp export-epsc2;', 'Separator', 'off');
   uimenu(f1, 'Label', '&Exit', 'Callback', 'imgcmp EXIT;', 'Separator', 'On');
   
   % Data base manipulation menu
   f2 = uimenu('Label','&Map', 'Position', 2, 'Callback','imgcmp mapopen;' );
   
   % Display menu
     f3 = uimenu('Label', '&Select', 'Position', 3, 'Callback', 'imgcmp select;', 'Tag', 'Selection');
   f4 = uimenu('Label', '&Print', 'Position', 4, 'Callback', 'imgcmp print;', 'Tag', 'ImagePrint');
   f5 = uimenu('Label', 'Print&All', 'Position', 5, 'Callback', 'imgcmp printall;', 'Tag', 'ImagePrintAll');
   
end;

if(exist('cmd', 'var') == 0)
   return;
end;

switch(cmd)
   
case 'open' % open the data file
   hf = findobj('tag', 'Map');
   if(~isempty(hf))
      close(hf);
   end;
   datac('open');
   df = datac('getdfile');
   imgcmp('mapopen', df.fullfile);
   return;
   
case 'accumulate' % change status of accumuation flag.
   h = findobj('Tag', 'accumulate');
   h1 = get(h);
   if(strmatch(get(h, 'Check'), 'on', 'exact'))
      set(h, 'Check', 'off');
   else
      set(h, 'Check', 'on');
   end;
   
case 'clear'
   h = findobj('Tag', 'image_compare');
   if(isempty(h))
      return; % require the base window to be UP.
   end;
   figure(h);
   hax = findobj(h, 'Type', 'axes');
   delete(hax);
   
case 'pagesetup'
   h = findobj('Tag', 'image_compare');
   pagesetupdlg(h);
   
   
case 'printsetup'
   h = findobj('Tag', 'image_compare');
   printdlg(h);
   
case 'print'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   print(h);
   set(hg, 'Visible', 'on');
   
   
case 'export-fig'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   savefig(h, 'fig');
   set(hg, 'Visible', 'on');
   
case 'export-m'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   savefig(h, 'm');
   set(hg, 'Visible', 'on');
   
case 'export-ill'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   savefig(h, 'ill');
   set(hg, 'Visible', 'on');
   
   
case 'export-wmf'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   saveas(h, 'fig', 'emf');
   set(hg, 'Visible', 'on');
   
case 'export-epsc2'
   h = findobj('Tag', 'image_compare');
   hg = findobj('Tag', 'Image_info');
   set(hg, 'Visible', 'off');
   savefig(h, 'epsc2');
   set(hg, 'Visible', 'on');
   
   
case 'EXIT'
   h = findobj('Tag', 'image_compare');
   if(~isempty(h))
      delete(h);
      return;
   end;
   
   
case 'mapfile'
   if(nargin > 1)
      [path f e] = fileparts(n);
      path = [path '\']; % needs appending...
      filename = [f '.map'];
   else
      [filename, path] = uigetfile([datac('getpath'), '*.map'], 'Open Map File');
      if(~ischar(filename))
         %	   msgbox(sprintf('Unable to open file: %s', filename), 'Map file open error', 'error');
         return;
      end
   end;
   if(exist([path filename], 'file') ~= 2)
      msgbox(sprintf('Unable to open file: %s', filename), 'Map file open error', 'error');
      return;
   end;
   [x1, x2] = textread([path filename], '%s %s');
   X = [x1, x2];
   h = findobj;
   % now put the list in the selection drop down.
   hfig = findobj('Tag', 'MapListControl');
   ml = {};
   k = 1;
   for i = 1:size(X,1)
      s=X{i,1};
      if(s(1) ~= ';' && s(1) ~= '#')
         ml{k} = sprintf('%-4s  %12s', X{i,1}, X{i,2}); % get the protocol if selected...
         k = k + 1;
      end;
   end;
   hfig = findobj('Tag', 'MapListControl');
   set(hfig, 'String', ml);
   set(hfig, 'Value', 1);
   % update window to tell us we got the data...
   h = findobj('Tag', 'image_compare');
   % set(h, 'Name', sprintf('Comparing - %s', f));
   
   
   
case 'mapopen'
   hf = findobj('tag', 'Map');
   if(isempty(hf))
      image_map;
      hfig = findobj('Tag', 'MapListControl');
      set(hfig, 'String', '');
      set(hfig, 'value', 1);
      hfig = findobj('Tag', 'MapImages');
      set(hfig, 'String', '');
      set(hfig, 'value', 1);
      hfig = findobj('Tag', 'MapRecord');
      set(hfig, 'String', '');
      set(hfig, 'value', 1);
      
      
      imgcmp('mapload');   
   else
      figure(hf);
   end;
   
   imgcmp('mapfile');
   return;
   
case 'mapclose'
   hf = findobj('tag', 'Map');
   if(~isempty(hf))
      close(hf);
   end;
   return
   
case 'mapsave'
   [filename, path] = uiputfile([datac('getpath'), '*.map'], 'Save Map File');
   if(~ischar(filename))
      %	   msgbox(sprintf('Unable to open file: %s', filename), 'Map file open error', 'error');
      return;
   end
   [p f e] = fileparts([path filename]);
   [p f '.map']   
   hfile = fopen([p '\' f '.map'], 'wt');
   if(isempty(hfile))
      return;
   end;
   
   hfig = findobj('tag', 'MapListControl');
   x = get(hfig, 'String');
   for i = 1:length(x)
      [rec{i} imagen{i}] = strtok(char(x{i,:}), ' ');
   end;
   recn = str2num(char(rec));
   [recn, j] = sort(recn);
   rec = rec(j);
   imagen = imagen(j);
   for i = 1:length(rec)
      fprintf(hfile, '%-4s  %12s\n', char(rec(i)), unblank(char(imagen(i))));
   end;
   fclose(hfile);
   
case 'mapload'
   % put in the record numbers in record list and the available .mat files starting with a number 
   % in the image list.
   nf=datac('getnote');
   rll = [nf.frec];
   for i = 1:length(rll)
      rl{i} = sprintf('%d', rll(i));
   end;
   
   hrec = findobj('tag', 'MapRecord');
   if(isempty(hrec))
      return;
   end;
   set(hrec, 'String', rl);
   set(hrec, 'value', 1);
   
   thisdir = datac('getpath'); % files are from same directory
   matfs = dir([thisdir '\' '*.mat']);
   iml = {};
   imls = {};
   k = 1;
   for i = 1:length(matfs)
      name = matfs(i).name;
      if(isnumber(name(1)))
         [p f e] = fileparts(name);
         %if(~strcmp(iml, f))
         iml{k} = deblank(f);
         k = k + 1;
         %end;
         
      end;
   end;
   % sort image list
   imls = sort(iml);
   iml
   imls
   himg = findobj('tag', 'MapImages');
   if(isempty(himg))
      return;
   end;
   set(himg, 'String', imls);
   set(himg, 'Value', 1);
   return;
   
   
case 'mapclear'
   hfig = findobj('Tag', 'MapListControl');
   set(hfig, 'String', '');
   set(hfig, 'value', 1);
   
case 'mapremove' % remove one entry
   hfig = findobj('tag', 'MapListControl');
   x = get(hfig, 'String');
   p = get(hfig, 'value');
   xn = {};
   k = 1;
   for i = 1:length(x)
      if(~any(p == i))
         xn{k} = x{i,:};
         k = k + 1;
      end;
   end;
   set(hfig, 'string', xn);
   set(hfig, 'value', min(p));
   return
   
case 'mapadd'
   newstr = get_select;
   if(isempty(newstr))
      return;
   end;
   hfig = findobj('tag', 'MapListControl');
   x = get(hfig, 'String');
   x{length(x)+1} = newstr;
   % sort into ascending list first, on record number.
   for i = 1:length(x)
      [rec{i} imagen{i}] = strtok(char(x{i}), ' ');
   end;
   recn = str2num(char(rec));
   [recn, j] = sort(recn);
   rec = rec(j);
   imagen = imagen(j);
   for i = 1:length(rec)
      x{i} = sprintf('%-4s  %12s', char(rec(i)), unblank(char(imagen(i))));
   end;
   
   
   set(hfig, 'string', x);
   set(hfig, 'value', length(x));
   return;
   
case 'mapreplace' % replace current entry with new selection
   hfig = findobj('tag', 'MapListControl');
   x = get(hfig, 'String');
   p = get(hfig, 'value');
   newstr = get_select; % get the selection from the record and image lists.
   if(isempty(newstr))
      return;
   end;
   x{p,:} = newstr;
   set(hfig, 'string', x);
   set(hfig, 'value', p);
   return
   
   
   
   
case 'view'
   if(~exist('n', 'variable'))
      hfig = findobj('Tag', 'MapListControl');
      n = get(hfig, 'value');
      X = get(hfig, 'String');
   end;
   [rec imagen] = strtok(char(X{n,:}), ' ');
   imagen = unblank(imagen);
   datac('retrieve', str2double(rec));
   image_compare([datac('getpath') imagen]);
   
case 'printall' % print all files in the selection list
   hfig = findobj('Tag', 'MapListControl');
   X = get(hfig, 'String');
   for n = 1:length(X)
      [rec imagen] = strtok(char(X{n,:}), ' ');
      imagen = unblank(imagen);
      datac('retrieve', str2double(rec));
      image_compare([datac('getpath') imagen]);
      hg = findobj('Tag', 'Image_info');
      set(hg, 'Visible', 'off');
      print(h);
      set(hg, 'Visible', 'on');
   end;
   
otherwise
end;


function [sel] = get_select()
%
% get the selection as a string from the first two lists.

sel = [];
hrec = findobj('tag', 'MapRecord');
if(isempty(hrec))
   return;
end;
himg = findobj('tag', 'MapImages');
if(isempty(himg))
   return;
end;

recl = get(hrec, 'string');
rn = recl(get(hrec, 'value'),:);
img = get(himg, 'string');
imgn = img(get(himg, 'value'),:);
sel = sprintf('%-4s  %12s', char(rn), char(imgn));
return;

